﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic;
using System.Collections;

public partial class userControls_RollupFilters : System.Web.UI.UserControl
{
    private string lnkbtn = string.Empty;


    public string ErrorMessage
    {
        set
        {
            lblErrorMessage.Text = value;
            tdErrorMessage.Style["display"] = "";
        }
    }
    private string _ReportName;
     
    public string ReportName
    {
        get { return _ReportName; }
        set { _ReportName = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (txtFrom.Text == "")
            {
                if (Application["CacheDate"]!=null)
                {
                txtFrom.Text = DateAndTime.Month(Convert.ToDateTime(Application["CacheDate"])) + "/01/" + DateAndTime.Year(Convert.ToDateTime(Application["CacheDate"]));
                txtTo.Text = Application["CacheDate"].ToString();}
                optRegionMarket.Checked = true;
                //chlProducts.Items[0].Selected = true;
            }

            else
            {
                calStart.SelectedDate = Convert.ToDateTime(txtFrom.Text);
                calEnd.SelectedDate = Convert.ToDateTime(txtTo.Text);

                ddlManagers.DataBind();
                ddlMarkets.DataBind();

                getCookie(txtFrom.ID, txtFrom);
                getCookie(txtTo.ID, txtTo);
                getCookie(optRegionMarket.ID, optRegionMarket);
                getCookie(ddlMarkets.ID, ddlMarkets);
                getCookie(optAcctManager.ID, optAcctManager);
                getCookie(ddlManagers.ID, ddlManagers);
                getCookie(chlLocations.ID, chlLocations);
                getCookie(chlDwellings.ID, chlDwellings);
                getCookie(chlProducts.ID, chlProducts);
                getCookie(chlProducts2.ID, chlProducts2);
                getCookie(ddlManagers.ID, ddlManagers);
            }

        }
        ConfigureReportParametersDialog();

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        ConfigureReportParametersDialog();

    }

    private void ConfigureReportParametersDialog()
    {
        HtmlTable[] tbls = new HtmlTable[] { tblAcctMgr, tblDateRange, tblDetails, tblDwelling, tblNational, tblProduct, tblRegionMarket };
        ArrayList alAllParms = new ArrayList { "ACCT_MGR", "START_DATE", "ByProductOrChannel", "DWELLING", "NATIONAL", "PRODUCT", "REGION" };
        if (ReportName == null) return;
        if (ReportName.Contains("&")) return;

        //Hide all controls first
        foreach (HtmlTable tbl in tbls)
        {
            tbl.Style["display"] = "none";

        }        
        
        //Use the Reportname to get the SQL
        string sSQL = SQLFiles.ConstructReportsSQL(ReportName, "");

        //Use the SQL to get the Parameters
        string[] saSQL = sSQL.Split(' ', ',');
        ArrayList alParms = new ArrayList();

        foreach (string sParm in saSQL)
            if (sParm.Trim().StartsWith("@"))
                alParms.Add(sParm.Trim().Substring(1));


        //Use the parameters to show the parameter input table
        foreach (string sParm in alParms)
            if (alAllParms.Contains(sParm))
                tbls[alAllParms.IndexOf(sParm)].Style["display"] = "";
    }


    protected void chkAllDimensions_CheckedChanged(object sender, EventArgs e)
    {
        //for (int i = 0; i < chlProducts.Items.Count; i++) chlProducts.Items[i].Selected = chkAllDimensions.Checked;
        //for (int i = 0; i < chlLocations.Items.Count; i++) chlLocations.Items[i].Selected = chkAllDimensions.Checked;
        //for (int i = 0; i < chlDwellings.Items.Count; i++) chlDwellings.Items[i].Selected = chkAllDimensions.Checked;
        //RefreshMetadata(sender, e);
    }

    protected void getCookie(string psCookieName, TextBox txt)
    {
        string sCookie = psCookieName + "_Report";
        txt.Text = Request.Cookies[sCookie] == null ? "" :
        Request.Cookies[sCookie].Value;
    }

    protected void getCookie(string psCookieName, CheckBox chk)
    {
        string sCookie = psCookieName + "_Report";
        chk.Checked = Request.Cookies[sCookie] == null ? false :
       Convert.ToBoolean(Request.Cookies[sCookie].Value);
    }

    protected void getCookie(string psCookieName, RadioButtonList optlst)
    {
        string sCookie = psCookieName + "_Report";
        optlst.SelectedIndex = Request.Cookies[sCookie] == null ? 0 :
       Convert.ToInt16(Request.Cookies[sCookie].Value);
    }

    protected void getCookie(string psCookieName, CheckBoxList chklst)
    {
        string sCookie = psCookieName + "_Report";

        string[] saSelected = Request.Cookies[sCookie] == null ? new string[0] :
       Request.Cookies[sCookie].Value.ToString().Split(',');
        for (int i = 0; i < saSelected.Length; i++)
            chklst.Items[i].Selected = Convert.ToBoolean(saSelected[i]);

    }

    protected void getCookie(string psCookieName, DropDownList ddl)
    {
        string sCookie = psCookieName + "_Report";
        ddl.SelectedIndex = Request.Cookies[sCookie] == null ? 0 :
            Convert.ToInt16(Request.Cookies[sCookie].Value.ToString());
    }

    protected void setCookie(string psCookie, string psValue, DateTime pdtExp)
    {
        string sCookie = psCookie + "_Report";
        if (Request.Cookies[sCookie] == null)
            Response.Cookies.Add(new HttpCookie(sCookie, psValue));
        else
            Response.Cookies[sCookie].Value = psValue;
        Request.Cookies[sCookie].Value = psValue;
        Response.Cookies[sCookie].Expires = pdtExp;
    }


}